package com.gm.app.web;

import java.util.List;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.gm.app.data.ResidentDAO;
import com.gm.app.model.Address;
import com.gm.app.model.Contact;
import com.gm.app.model.Place;
import com.gm.app.model.Resident;

@Controller
@RequestMapping("/resident")
public class ResidentController extends RegisterController {

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String get(@PathVariable("id") Long id, Model model) {
		Resident i = Resident.findResident(id);
		model.addAttribute("resident", i == null ? new Resident() : i);
		return "resident/view";
	}

	@RequestMapping(method = RequestMethod.GET)
	public String list(
			Model model,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "address", required = false) String address,
			@RequestParam(value = "country", required = false) String country,
			@RequestParam(value = "comments", required = false) String comments,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "index", required = false) Integer index) {
		List<Resident> list = ResidentDAO.search(name, address, country,
				comments, page, index).getResultList();
		model.addAttribute("resident", list);
		return "resident/list";
	}

	@RequestMapping(method = RequestMethod.PUT)
	public String add(@Valid Resident i, Model model) {
		i.persist();
		return "redirect:/resident/" + i.getId();
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable("id") Long id, Model model) {
		Resident i = Resident.findResident(id);
		if (i != null) {
			i.remove();
		}
		return "redirect:/resident";
	}

	@SuppressWarnings("unchecked")
	protected <T extends Place> T get(final T place) {
		if (place != null) {
			if (place.getId() != null) {
				return (T) Place.findPlace(place.getId());
			} else {
				place.persist();
				return place;
			}
		} else
			return null;
	}

	@RequestMapping(method = RequestMethod.POST)
	@Transactional
	public String update(Resident i, BindingResult binding, Model model) {
		if (binding.hasErrors()) {
			model.addAttribute("resident", i);
			return "/resident/view";
		}
		/*Contact c = i.getContact();
		if (c != null)
			c.setCountry(get(c.getCountry()));

		Address a = i.getAddress();

		if (a != null) {
			a.setCountry(get(a.getCountry()));
			a.setCity(get(a.getCity()));
			a.setDistrict(get(a.getDistrict()));
			a.setState(get(a.getState()));
			a.setStreet(get(a.getStreet()));
		}*/

		i = (Resident) i.merge();
		return "redirect:/resident/" + i.getId();
	}

	@RequestMapping(value = "/search")
	public String search(Model model,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "address", required = false) String address,
			@RequestParam(value = "country", required = false) String country,
			@RequestParam(value = "comments", required = false) String comments) {
		List<Resident> q = null;
		q = ResidentDAO.search(name, address, country, comments)
				.getResultList();
		model.addAttribute("resident", q);
		return "resident/list";
	}
}
